package com.ruoyi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.domain.HisCart;
import com.ruoyi.domain.HisMedicineDrug;
import com.ruoyi.domain.MedicineFactory;
import com.ruoyi.mapper.HisMedicineCartMapper;
import com.ruoyi.mapper.HisMedicineDrugMapper;
import com.ruoyi.mapper.HisMedicineFactoryMapper;
import com.ruoyi.service.IHisMedicineCartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @author rk
 * @description: TODO
 * @date 2024/9/1 16:50
 */
@Service
public class HisMedicineCartServiceImpl implements IHisMedicineCartService {
    @Autowired
    private HisMedicineCartMapper cartMapper;
    @Autowired
    private HisMedicineDrugMapper drugMapper;
    @Autowired
    private HisMedicineFactoryMapper factoryMapper;

    @Override
    public int addDrugToCart(Long userId, Long[] drugIds) throws JsonProcessingException {
        HisCart hisCartByUid = cartMapper.selectCartByUserId(userId);
        HisCart hisCart = new HisCart();
        hisCart.setUserId(userId);
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<Long,HisMedicineDrug> hashMap = new HashMap<Long,HisMedicineDrug>();


        if (hisCartByUid==null){
            for (Long drugId : drugIds) {
                HisMedicineDrug hisMedicineDrug = drugMapper.selectHisMedicineDrugByDrugId(drugId);
                MedicineFactory medicineFactory = factoryMapper.selectFactoryById(hisMedicineDrug.getFactoryId());
                hisMedicineDrug.setFactory(medicineFactory);
                hisMedicineDrug.setDrugCarNum(1L);
                hashMap.put(drugId,hisMedicineDrug);
            }
            String map = objectMapper.writeValueAsString(hashMap);
            hisCart.setCart(map);
          return   cartMapper.addDrugToCart(hisCart);
        }

        String cart = hisCartByUid.getCart();
        HashMap<Long, HisMedicineDrug> hashMapNew = objectMapper.readValue(cart, new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });
        for (Long drugId:drugIds) {
            if (!hashMapNew.containsKey(drugId)) {
                HisMedicineDrug hisMedicineDrug = drugMapper.selectHisMedicineDrugByDrugId(drugId);
                MedicineFactory medicineFactory = factoryMapper.selectFactoryById(hisMedicineDrug.getFactoryId());
                hisMedicineDrug.setFactory(medicineFactory);
                hisMedicineDrug.setDrugCarNum(1L);
                hashMapNew.put(drugId, hisMedicineDrug);
            }
        }

        String s = objectMapper.writeValueAsString(hashMapNew);
        hisCart.setCart(s);
        return cartMapper.updateCart(hisCart);
    }

    @Override
    public List<HisMedicineDrug> selectCartByUserId(Long userId) throws JsonProcessingException {
        HisCart hisCart = cartMapper.selectCartByUserId(userId);
        ObjectMapper objectMapper = new ObjectMapper();

        HashMap<Long, HisMedicineDrug> hisMedicineDrugHashMap = objectMapper.readValue(hisCart.getCart(), new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });

      Collection collection = (Collection) hisMedicineDrugHashMap.values();
      ArrayList<HisMedicineDrug> arrayList = new ArrayList<>(collection);
      return arrayList;
    }

    @Override
    public int deleteCartByCarId(Long[] drugIds,Long userId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        HisCart hisCart = cartMapper.selectCartByUserId(userId);
        String cart = hisCart.getCart();
        HashMap<Long, HisMedicineDrug> hisMedicineDrugHashMap = objectMapper.readValue(cart, new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });
        for (Long drugId : drugIds) {
            hisMedicineDrugHashMap.remove(drugId);
        }
        String s = objectMapper.writeValueAsString(hisMedicineDrugHashMap);
        hisCart.setCart(s);
        hisCart.setUserId(userId);
        return cartMapper.updateCart(hisCart);

    }

    @Override
    public int setCartDrugNum(HisMedicineDrug hisMedicineDrug, Long userId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        HisCart hisCart = cartMapper.selectCartByUserId(userId);
        String cart = hisCart.getCart();
        HashMap<Long, HisMedicineDrug> hisMedicineDrugHashMap = objectMapper.readValue(cart, new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });
        HisMedicineDrug drug = hisMedicineDrugHashMap.get(hisMedicineDrug.getDrugId());
        drug.setDrugCarNum(Long.valueOf(hisMedicineDrug.getDrugCarNum().toString()));
        hisMedicineDrugHashMap.put(hisMedicineDrug.getDrugId(),drug);
        String s = objectMapper.writeValueAsString(hisMedicineDrugHashMap);
        hisCart.setCart(s);
        hisCart.setUserId(userId);
        return cartMapper.updateCart(hisCart);
    }

    @Override
    public int setCartDrugFactory(HisMedicineDrug hisMedicineDrug, Long userId) throws JsonProcessingException {
        //拿到要设置的工厂对象
        ObjectMapper objectMapper = new ObjectMapper();
        MedicineFactory medicineFactory = factoryMapper.selectFactoryById(hisMedicineDrug.getFactoryId());

        //拿到map中的car中的drug对象
        HisCart hisCart = cartMapper.selectCartByUserId(userId);
        String cart = hisCart.getCart();
        HashMap<Long, HisMedicineDrug> hisMedicineDrugHashMap = objectMapper.readValue(cart, new TypeReference<HashMap<Long, HisMedicineDrug>>() {
        });
        HisMedicineDrug medicineDrug = hisMedicineDrugHashMap.get(hisMedicineDrug.getDrugId());
        //设置drug中的工厂对象
        medicineDrug.setFactory(medicineFactory);
        hisMedicineDrugHashMap.put(hisMedicineDrug.getDrugId(),medicineDrug);
        String s = objectMapper.writeValueAsString(hisMedicineDrugHashMap);
        hisCart.setCart(s);
        hisCart.setUserId(userId);
        return cartMapper.updateCart(hisCart);
    }
}
